package cn.itsource.pethome.service.impl;

import cn.hutool.crypto.SecureUtil;
import cn.itsource.pethome.Util.EmailUtil;
import cn.itsource.pethome.constants.BaseConstants;
import cn.itsource.pethome.domain.BaseDomain;
import cn.itsource.pethome.domain.Department;
import cn.itsource.pethome.domain.Employee;
import cn.itsource.pethome.domain.Shop;
import cn.itsource.pethome.mapper.EmployeeMapper;
import cn.itsource.pethome.mapper.ShopMapper;
import cn.itsource.pethome.query.DepartmentQuery;
import cn.itsource.pethome.query.ShopQuery;
import cn.itsource.pethome.result.JSONResult;
import cn.itsource.pethome.result.PageList;
import cn.itsource.pethome.service.ShopService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.common.BaseMapper;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

/**
 * @description:
 * @auth: wujiangbo
 * @date: 2022-02-19 11:24
 */
@Service
@Transactional
public class ShopServiceImpl extends BaseServiceImpl <Shop,ShopQuery> implements ShopService {

    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private  EmailUtil emailUtil;
    //店铺入住/注册
    @Override
    public JSONResult settlement(Shop shop) {
        /**
         * 思路：
         * 1、入参判断
         *   1.1、判断字段是否为空
         *   1.2、店铺名是否已存在？邮箱，座机，手机，用户名，地址
         *   1.3、手机号是否合法，邮箱是否合法
         * 2、保存管理员信息到employee表
         * 3、保存商家信息到shop表，并设置管理员的外键
         */
        // 1.判断参数 ,这里省略部分判断
        if(!StringUtils.hasLength(shop.getName())){
            throw new RuntimeException("店铺名不可为空");
        }
        //店铺是否已经被注册
        Shop shopFromDB = shopMapper.selectByName(shop.getName());
        if(null != shopFromDB){
            throw new RuntimeException("店铺名已经被注册，请更换");
        }

        //..省略其他判断..

        //2.保存管理员
        Employee admin = shop.getAdmin();
        admin.setPassWord(SecureUtil.md5(admin.getPassWord()));
        admin.setState(BaseConstants.EmployeeConstants.STATE_WAIT_ACTIVE);
        admin.setAge(null);
        employeeMapper.insert(admin);

        //3.保存店铺数据
        shop.setRegisterTime(new Date());
        shop.setState(BaseConstants.ShopConstants.STATE_WAIT_AUTID);
        shop.setAdminId(admin.getId());
        shopMapper.insert(shop);

        //返回结果
        return JSONResult.success();
    }

    @Override
    public JSONResult auditYes(Long shopId) {
        //入参校验

        //更改状态
        Shop shop = shopMapper.selectByPrimaryKey(shopId);
        //修改值
        shop.setState(BaseConstants.ShopConstants.STATE_WAIT_ACTIVE);
        //入库
        shopMapper.updateByPrimaryKey(shop);
        //发邮件
        //获取管理员邮箱

        Example example = new Example(Employee.class);
        example.and().andEqualTo("id",shop.getAdminId());
        Employee employee = employeeMapper.selectOneByExample(example);

        String msg ="请点击下面连接,进行激活!<a href='http://127.0.1:80/shop/jihuo/"  + shopId + "'>点我激活</a>";
        emailUtil.sendMime("审核通过",msg,employee.getEmail(),null);
        return JSONResult.success();
    }

    @Override
    public JSONResult pagelist(ShopQuery shopQuery) {
        Page<Shop> startPage = PageHelper.startPage(shopQuery.getCurrent(), shopQuery.getSize());
        List<Shop> shopList = shopMapper.queryData(shopQuery);

        return   JSONResult.success(new PageInfo<>(shopList));
    }

    @Override
    public List<Shop> list() {
        return shopMapper.list();
    }

    @Override
    public JSONResult jihuo(Long shopId) {
        Shop shop = shopMapper.selectByPrimaryKey(shopId);
        shop.setState(BaseConstants.ShopConstants.STATE_ACTIVE_SUCCESS);
        int i = shopMapper.updateByPrimaryKey(shop);
        return JSONResult.success();
    }
}